package com.grapevine.contact.adapter;

import android.content.Context;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.CursorAdapter;
import android.widget.QuickContactBadge;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AbsListView.OnScrollListener;

import com.grapevine.contact.R;
import com.grapevine.contact.util.ContactPhotoLoader;

public class ContactAdapter extends CursorAdapter implements OnScrollListener
{
	private ContactPhotoLoader mContactPhotoLoader;

	private int width;
	private int height;

	public ContactAdapter(Context context, Cursor c, ContactPhotoLoader contactPhotoLoader)
	{
		super(context, c);
		mContactPhotoLoader = contactPhotoLoader;
		Drawable drawable = context.getResources().getDrawable(mContactPhotoLoader.mDefaultResourceId);
		width = drawable.getMinimumWidth() + 15;
		height = drawable.getMinimumHeight() + 15;
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor)
	{

		QuickContactBadge photo = (QuickContactBadge) view.findViewById(R.id.contact_photo);

		photo.setLayoutParams(new RelativeLayout.LayoutParams(width, height));

		TextView name = (TextView) view.findViewById(R.id.contact_name);
		String nameText = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
		name.setText(nameText);

		int id = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts._ID));

		int photoID = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));

		// final long contactId = cursor.getLong(0);
		// final String lookupKey = cursor.getString(6);
		// photo.assignContactUri(Contacts.getLookupUri(contactId, lookupKey));

		photo.assignContactUri(Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(id)));

		mContactPhotoLoader.loadPhoto(photo, photoID);

		// int width = b.getWidth();
		// int height = b.getHeight();

		// Bitmap bitmap = ContactPhoto.getPhoto(id, context);

		/**
		 * 
		 * final long contactId = cursor.getLong(SUMMARY_ID_COLUMN_INDEX); final
		 * String lookupKey = cursor.getString(SUMMARY_LOOKUP_KEY);
		 * cache.photoView.assignContactUri(Contacts.getLookupUri(contactId,
		 * lookupKey));
		 * 
		 * photoView.layout( leftBound, photoTop, leftBound + mPhotoViewWidth,
		 * photoTop + mPhotoViewHeight);
		 */

		// photo.setLayoutParams(params)
		// photo.layout(0, 0, 80, 76);

		// if (null != bitmap)
		// {

		// bitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeByteArray(
		// contactIcon, 0, contactIcon.length, null), width, height,
		// true);
		// photo.setI
		// android:src setImageResource(int) Sets a drawable as the content of
		// this ImageView.

		// photo.setImageBitmap(bitmap);

		// Drawable drawable = photo.getDrawable();

		// BitmapDrawable drawable =(BitmapDrawable) photo.getDrawable();

		// photo.getWidth();

		// photo.getHeight();

		// Bitmap b = photo.getDrawingCache();

		/**
		 * TypedArray a = mContext.obtainStyledAttributes(null,
		 * com.android.internal.R.styleable.ViewGroup_Layout,
		 * QUICK_CONTACT_BADGE_STYLE, 0); mPhotoViewWidth =
		 * a.getLayoutDimension(
		 * android.R.styleable.ViewGroup_Layout_layout_width,
		 * ViewGroup.LayoutParams.WRAP_CONTENT); mPhotoViewHeight =
		 * a.getLayoutDimension(
		 * android.R.styleable.ViewGroup_Layout_layout_height,
		 * ViewGroup.LayoutParams.WRAP_CONTENT); a.recycle();
		 */

		// TypedArray a = context.obtainStyledAttributes(null,
		// ViewGroup_Layout,
		// 0, 0);

		// photo.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 80, 76,
		// true));

		// photo.setImageBitmap(bitmap);

		// photo.setImageBitmap(bitmap);

		// photo.setImageDrawable(new
		// BitmapDrawable(Bitmap.createScaledBitmap(bitmap,photo.getWidth(),photo.getHeight(),true)));

		// }
		// else
		// {

		// Drawable drawable
		// =context.getResources().getDrawable(R.drawable.ic_contact_picture);
		// photo.setImageResource(R.drawable.ic_contact_picture);

		// photo.setImageDrawable(drawable);

		// photo.setImageDrawable(null);
		// }

		// final Context c = context;
		// view.findViewById(R.id.contact_sms).setOnClickListener(new
		// OnClickListener()
		// {
		// @Override
		// public void onClick(View v)
		// {
		// Cursor phones =
		// c.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
		// null,
		// ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id, null,
		// null);
		// if (phones.moveToFirst())
		// {
		// String number =
		// phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
		// Uri smsToUri = Uri.parse("smsto:" + number);
		// Intent smsIntent = new Intent(Intent.ACTION_SENDTO, smsToUri);
		// c.startActivity(smsIntent);
		// }
		// phones.close();
		// }
		// });
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent)
	{
		View convertView = LayoutInflater.from(context).inflate(R.layout.contact_item, null);
		return convertView;
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState)
	{
		if (scrollState == OnScrollListener.SCROLL_STATE_FLING)
		{
			mContactPhotoLoader.pause();
		}
		else
		{
			mContactPhotoLoader.resume();
		}

	}

}